package com.ffox.service.impl;

import com.ffox.entity.GroupChatFile;
import com.ffox.entity.GroupChatMessage;
import com.ffox.entity.GroupChatNotification;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.mapper.UserGroupchatMapper;
import com.ffox.service.UserGroupchatService;
import com.ffox.service.dto.MemberDto;
import com.ffox.service.dto.MessageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserGroupchatServiceImpl implements UserGroupchatService {
    @Autowired
    private FastDfsUtil fastDfsUtil;

    @Autowired
    private UserGroupchatMapper userGroupchatMapper;
    @Override
    public MessageDto sendMessage(GroupChatMessage message) {
        message.setSendTime(LocalDateTime.now());
        message.setStatus((byte) 0);

        userGroupchatMapper.insertMessage(message);
        GroupChatMessage lastMessage = userGroupchatMapper.getLastMessage();
        MessageDto messageDto = new MessageDto();
        messageDto.setId(lastMessage.getId());
        messageDto.setClassId(lastMessage.getClassId());
        messageDto.setUserNum(lastMessage.getUserNum());
        messageDto.setAvatar(userGroupchatMapper.getUserAvatarByNumber(message.getUserNum()));
        messageDto.setContent(lastMessage.getContent());
        messageDto.setType(lastMessage.getType());
        messageDto.setSendTime(lastMessage.getSendTime());
        return messageDto;
    }

    @Override
    public Integer deleteMessage(Long messageId) {
        return userGroupchatMapper.updateMessage(messageId, (byte) 1);
    }

    @Override
    public List<MessageDto> getMessages(Long classId) {
        List<GroupChatMessage> messages = userGroupchatMapper.getMessages(classId);
        List<MessageDto> messageDtos = new ArrayList<>();

        for (int i=0; i < messages.size(); i++){
            MessageDto messageDto = new MessageDto();
            messageDto.setId(messages.get(i).getId());
            messageDto.setClassId(messages.get(i).getClassId());
            messageDto.setUserNum(messages.get(i).getUserNum());
            messageDto.setAvatar(userGroupchatMapper.getUserAvatarByNumber(messages.get(i).getUserNum()));
            messageDto.setContent(messages.get(i).getContent());
            messageDto.setType(messages.get(i).getType());
            messageDto.setSendTime(messages.get(i).getSendTime());
            messageDtos.add(messageDto);
        }

        return messageDtos;
    }

    @Override
    public Integer uploadFile(Long classId,String userNum, MultipartFile files) {
        String str = null;
        GroupChatFile file = new GroupChatFile();
        file.setClassId(classId);
        file.setUserNum(userNum);
        try {
            str = URLDecoder.decode(files.getOriginalFilename(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        int lastDotIndex = str.lastIndexOf('.');
        String beforeLastDot = str.substring(0, lastDotIndex);
        String afterLastDot = str.substring(lastDotIndex+1);
        file.setFileName(beforeLastDot);
        file.setFileType(afterLastDot);
        file.setFileSize(files.getSize());
        try {
            byte[] filesBytes = files.getBytes();
            String url = fastDfsUtil.upload_file(filesBytes, afterLastDot);
            file.setFilePath(url);
            file.setCreatedAt(LocalDateTime.now());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }



        return userGroupchatMapper.insertFile(file);
    }

    @Override
    public Integer deleteFile(Long fileId) {
        return userGroupchatMapper.updateFile(fileId, (byte) 1, LocalDateTime.now());
    }

    @Override
    public List<GroupChatFile> getFiles(Long classId) {
        return userGroupchatMapper.getFiles(classId);
    }

    @Override
    public Integer createNotification(GroupChatNotification notification) {
        notification.setCreatedAt(LocalDateTime.now());
        return userGroupchatMapper.insertNotification(notification);
    }

    @Override
    public Integer deleteNotification(Long notificationId) {
        return userGroupchatMapper.updateNotification(notificationId, (byte) 1, LocalDateTime.now());
    }

    @Override
    public List<GroupChatNotification> getNotifications(Long classId) {
        return userGroupchatMapper.getNotifications(classId);
    }

    @Override
    public Integer markNotificationReadCount(Long notificationId) {
        System.out.println("功能未完善");
        return null;
    }

    @Override
    public Integer updateNotificationIsPinned(Long notificationId, Byte IsPinned) {
        return userGroupchatMapper.updateNotificationIsPinned(notificationId, IsPinned);
    }

    @Override
    public List<MemberDto> getClassMembers(Long classId) {
        return userGroupchatMapper.getClassMembers(classId);
    }
}
